package com.zmj.web;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zmj.biz.impl.UserServiceImpl;
import com.zmj.entity.User;
import com.zmj.utils.Message;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class UserController {

    @Autowired
    UserServiceImpl userService;


    @RequestMapping("/getOne")
    public String getOne(HttpSession session,Map<String,Object> map){
        User user= (User) session.getAttribute("user");
        User user1=userService.getOne(user.getUsername());
        map.put("userOne",user1);
        return "userInfo";
    }
    @RequestMapping("/getUser")
    public String getUser(String pageNum,Map<String,Object> map){
        if (pageNum==null){
            pageNum="1";
        }
        PageHelper.startPage(Integer.parseInt(pageNum),2);
        List<User> userList=userService.getUser();
        PageInfo pageInfo=new PageInfo(userList);
        map.put("pageInfo",pageInfo);
        return "adminList";
    }
    @RequestMapping("/login")
    public String login(User user, Map<String,Object> map, HttpSession session){
        User user1=userService.login(user);
        if(user1==null){
            map.put("message","登陆失败");
            return "login";
        }else {
            session.setAttribute("user",user1);
            if (user1.getIsAdmin()==1){
                return "index_admin";
            }else {
                return "index_user";
            }
        }

    }
    @RequestMapping("/register")
    public String register(MultipartFile img, User user, Map<String,Object> map, HttpSession session,HttpServletRequest request) throws IOException {

        if (!user.getPassword().equals(user.getRepassword())){
            map.put("message","两次密码不一致");
            return "register";
        }else {

            String path=session.getServletContext().getRealPath("images");
            System.out.println(path);
            File file=new File(path);
            if (!file.exists()){
                file.mkdir();
            }
            String savePath="";
            if (!img.isEmpty()){
                String fileName=img.getOriginalFilename();
                String suffix=fileName.substring(fileName.lastIndexOf("."));
                String newName= UUID.randomUUID().toString().replace("-","")+suffix;
                img.transferTo(new File(file,newName));
                savePath=request.getContextPath()+"/images/"+newName;
            }
            user.setImage(savePath);
            boolean flg= userService.register(user);
            if (flg){
                map.put("user",user);
                return "login";
            }else {
                return "register";
            }

        }

    }

    @RequestMapping("/download")
    public void downlaod(String path, HttpServletResponse response,HttpSession session,HttpServletRequest request) throws Exception {
        String realPath=session.getServletContext().getRealPath("images");
        String fileName=path.substring((request.getContextPath()+"/images/").length());
        FileInputStream fi=new FileInputStream(realPath+File.separator+fileName);
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
        IOUtils.copy(fi,response.getOutputStream());

    }

    @RequestMapping("/checkName")
    @ResponseBody
    public Message checkName(String username){
        Message message=new Message();
        User user=userService.checkName(username);

        if (user==null){
            message.setCode(1);
            message.setMessage("此用户名可用");
        }else {
            message.setCode(0);
            message.setMessage("此用户名已注册");
        }
        return message;
    }
    @RequestMapping("/checkEmail")
    @ResponseBody
    public Message checkEmail(String email){
        Message message=new Message();
        User user=userService.checkEmail(email);

        if (user==null){
            message.setCode(1);
            message.setMessage("此邮箱可用");
        }else {
            message.setCode(0);
            message.setMessage("此邮箱已注册");
        }
        return message;
    }

}
